package org.example.ch02.p02;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayList<String> idcards = new ArrayList<>();
        int n=Integer.parseInt(scanner.nextLine());
        for (int i=0;i<n;i++){
            idcards.add(scanner.nextLine());
        }
        while(true){
            String command=scanner.nextLine();
            if (command.equals("sort1")){
                ArrayList<String> dates=new ArrayList<>();
                for (String idcard:idcards){
                    String date = idcard.substring(6, 10) + "-" + idcard.substring(10, 12) + "-" + idcard.substring(12, 14);
                    dates.add(date);
                }
                Collections.sort(dates);
                for (String date:dates){
                    System.out.println(date);
                }
            } else if (command.equals("sorts")) {
                Collections.sort(idcards, new  Comparator<String>(){
                    @Override
                     public int compare(String id1, String id2){
                        String date1=id1.substring(6,14);
                        String date2=id2.substring(6,14);
                        return date1.compareTo(date2);
                    }
                });
                for (String idcard:idcards){
                    System.out.println(idcard);
                }
            }else{
                System.out.println("exit");
                break;
            }
        }
    }
}
